package com.erps.config;

import com.erps.config.Sys.SysUserRestController;
import com.erps.config.Sys.SystemController;
import com.erps.config.res.CourseController;
import com.erps.config.res.CustController;
import com.erps.config.res.HumanController;
import com.erps.config.res.OnLineController;
import com.erps.config.res.StandController;
import com.erps.db.DBType;
import com.erps.interceptor.CheckLoginInterceptor;
import com.erps.model.MessageModel;
import com.erps.model.ResRequestModel;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.handler.UrlSkipHandler;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.SqlReporter;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;

public class MainFinalConfig extends JFinalConfig {

	public void configConstant(Constants me) {
		loadPropertyFile("config.properties");
		me.setError404View("framework/error/404.html");
		me.setError500View("framework/error/500.html");
		me.setDevMode(getPropertyToBoolean("devMode", true));
		me.setEncoding("utf-8");
		me.setViewType(ViewType.FREE_MARKER);
	}

	public void configRoute(Routes me) {
		// 系统框架
		me.add("/", IndexController.class);
		me.add("/home", HomeController.class, "/home");

		me.add("/res_stand", StandController.class, "/res_stand");
		me.add("/res_human", HumanController.class, "/res_human");
		me.add("/res_course", CourseController.class, "/res_course");
		me.add("/res_cust", CustController.class, "/res_cust");
		me.add("/res_online", OnLineController.class, "/res_online");

		me.add("/ersp_sys", SystemController.class, "/ersp_sys");

		me.add("/achievement", AchievementController.class, "/achievement");

		// 在线消息
		me.add("/message", MessageController.class, "/message");

		// 后台管理
		me.add("/services", ServicesController.class, "/services");

		// 后台管理Rest
		me.add("/api", ServicesRestController.class);
		me.add("/request", SaveResRequestController.class, "/request");// 预约资源公共控制器
       
		me.add("/api/sys_user", SysUserRestController.class);
	}

	public void configPlugin(Plugins me) {
		String cMConnStr = getProperty(DBType.MASTER + ".ConnStr");
		String cMUserID = getProperty(DBType.MASTER + ".UserID");
		String cMPassword = getProperty(DBType.MASTER + ".Password");

		DruidPlugin vMasterDB = new DruidPlugin(cMConnStr, cMUserID, cMPassword);
		me.add(vMasterDB);

		String cSConnStr = getProperty(DBType.SLAVE + ".ConnStr");
		String cSUserID = getProperty(DBType.SLAVE + ".UserID");
		String cSPassword = getProperty(DBType.SLAVE + ".Password");

		DruidPlugin vSlaveDB = new DruidPlugin(cSConnStr, cSUserID, cSPassword);
		me.add(vSlaveDB);

		// 用于缓存news模块的redis服务
		ActiveRecordPlugin arp = new ActiveRecordPlugin(vMasterDB);
		arp.setShowSql(true);

		// 用于缓存news模块的redis服务
		arp.setContainerFactory(new CaseInsensitiveContainerFactory());
		//SqlReporter.setLog(true);
		
		me.add(arp);

		arp.addMapping("ersp_sys_msg", MessageModel.class);
		arp.addMapping("ersp_res_request", ResRequestModel.class);
		//arp.addMapping("ersp_res_info", ResRequestModel.class);
		//arp.addMapping("ersp_sys_org", ResRequestModel.class);
	}

	public void configInterceptor(Interceptors me) {
		me.add(new CheckLoginInterceptor());
		me.add(new SessionInViewInterceptor(true));
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("BASE_PATH"));
		me.add(new UrlSkipHandler("/ajaxpost", true));
		me.add(new UrlSkipHandler("/ajaxagent", true));
		me.add(new UrlSkipHandler("/ajaxpostagent", true));
		me.add(new UrlSkipHandler("/ajaxdelete", true));
		me.add(new UrlSkipHandler("/upload", true));

		// me.add(new UrlSkipHandler("/dwr", true));
	}

	public static void main(String[] args) {
		JFinal.start("WebRoot", 8012, "/", 5);
	}
}
